package com.example.hello;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class Category extends SQLiteOpenHelper {

	// All Static variables

	private static final int DATABASE_VERSION = 1;
	private static final String DATABASE_NAME = "testhooter";
	private static final String TABLE_NAME = "category";

	// category Table Columns names
	private static final String CAT_ID = "cat_id"; // incremented
	private static final String CAT_NAME = "cat_name";

	public Category(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	// Creating Tables
	@Override
	public void onCreate(SQLiteDatabase db) {
		String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_NAME + "("
				+ CAT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + CAT_NAME
				+ " char(30)" + ")";
		db.execSQL(CREATE_CONTACTS_TABLE);
	}

	// Upgrading database
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
		// Create tables again
		onCreate(db);
	}

	// Adding new Category
	void addCategory(String _cat_name) {
		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put(CAT_NAME, _cat_name);

		// Inserting Row
		db.insert(TABLE_NAME, null, values);
		db.close(); // Closing database connection
	}

	// Getting Category Details
	public List<Map<String, ArrayList<String>>> getCategoryDetails(String... _fields) {

		Map<String, ArrayList<String>> _pair = new HashMap<String, ArrayList<String>>();
		
		List<Map<String, ArrayList<String>>> _list = new ArrayList<Map<String, ArrayList<String>>>();
		
		String _temp_fields = "";
		for (int i = 0; i < _fields.length; i++) {
			Log.v("field name", _fields[i]);
			if (i == _fields.length - 1) {
				_temp_fields += _fields[i];
			} else {
				_temp_fields += _fields[i] + ",";
			}
		}
		Log.v("FIELDS NAMES ARE: ", _temp_fields);
		String selectQuery = "SELECT " + _temp_fields + " FROM " + TABLE_NAME;

		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);
		ArrayList<String> _arr_list = new ArrayList<String>();
		if (cursor.moveToFirst()) {
			do {				
				String _temp_field_name="";
				for (int i = 0; i < _fields.length; i++) {					
					_arr_list.add(cursor.getString(i));
					Log.v("Cursor",cursor.getString(i));
					_temp_field_name = _fields[i];					
				}
				_pair.put(_temp_field_name, _arr_list);								
			} while (cursor.moveToNext());
			_list.add(_pair);
		}

		// return contact list
		return _list;
	}

	// Getting Categories Count
	public String getCategoryCount() {
		String countQuery = "SELECT  * FROM " + TABLE_NAME;
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(countQuery, null);
		int _count = cursor.getCount();
		cursor.close();

		// return count
		return String.valueOf(_count);
	}

}